package lottery;

import java.awt.Color;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Random;

import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JSpinner;
import javax.swing.LayoutStyle;
import javax.swing.SpinnerListModel;
import javax.swing.SpinnerModel;
import javax.swing.WindowConstants;

import dao.DaoFactory;
import dao.User;
import dao.UserDao;

public class BuyTickets extends JFrame {

	User user4 = new User();
	String str;

	public BuyTickets(User user) {
		user4 = user;
		init();
		super.setTitle("彩票购买抽奖程序");
		int width = Toolkit.getDefaultToolkit().getScreenSize().width;
		int height = Toolkit.getDefaultToolkit().getScreenSize().height;
		super.setLocation((width - 400) / 2, (height - 350) / 2);

		this.setVisible(true);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}

	public void init() {

    	ArrayList<Integer> arr=new ArrayList();;
    	for(int i=0;i<37;i++) {
    		arr.add(i);
    	}
    	ArrayList<Integer> arr1=new ArrayList();;
    	for(int i=1;i<11;i++) {
    		arr1.add(i);
    	}
    	SpinnerModel model1 = new SpinnerListModel(arr);
    	SpinnerModel model2 = new SpinnerListModel(arr);
    	SpinnerModel model3 = new SpinnerListModel(arr);
    	SpinnerModel model4 = new SpinnerListModel(arr);
    	SpinnerModel model5 = new SpinnerListModel(arr);
    	SpinnerModel model6 = new SpinnerListModel(arr);
    	SpinnerModel model7 = new SpinnerListModel(arr);
    	SpinnerModel model8 = new SpinnerListModel(arr1);
    	JLabel label1 =  new JLabel();
    	JSpinner num1 = new JSpinner(model1);
    	JSpinner num2 = new JSpinner(model2);
    	JSpinner num3 = new JSpinner(model3);
    	JSpinner num4 = new JSpinner(model4);
    	JSpinner num5 = new JSpinner(model5);
    	JSpinner num6 = new JSpinner(model6);
    	JSpinner num7 = new JSpinner(model7);
    	JSpinner num8 = new JSpinner(model8);
    	JLabel label2 = new JLabel();
    	JLabel label3 = new JLabel();
    	JLabel label4 = new JLabel();
    	JLabel label5 = new JLabel();
    	JLabel label6 = new JLabel();
    	JLabel label7 = new JLabel();
    	JLabel label8 = new JLabel();
    	JLabel label9 = new JLabel();
    	JButton bt1 = new JButton();
    	JButton bt2 = new JButton();
    	JButton bt3 = new JButton();
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        
        getContentPane().setBackground(Color.CYAN);
        label1.setFont(new Font("宋体", 1, 20)); // NOI18N
        label1.setText("选取你的幸运号(每注2元)");

        label2.setFont(new Font("宋体", 1, 14)); 
        label2.setText("1");

        label3.setFont(new Font("宋体", 1, 14)); 
        label3.setText("2");

       label4.setFont(new java.awt.Font("宋体", 1, 14)); 
       label4.setText("3");

        label5.setFont(new Font("宋体", 1, 14)); 
        label5.setText("4");

        label6.setFont(new Font("宋体", 1, 14)); 
        label6.setText("5");

        label7.setFont(new Font("宋体", 1, 14)); 
        label7.setText("6");

        label8.setFont(new Font("宋体", 1, 14)); 
        label8.setText("7");
        label9.setFont(new Font("宋体", 1, 14)); 
        label9.setText("倍数");

        bt1.setText("确认购买");
        bt1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
            	String str1=num1.getValue().toString();
            	String str2=num2.getValue().toString();
            	String str3=num3.getValue().toString();
                String str4=num4.getValue().toString();
                String str5=num5.getValue().toString();
                String str6=num6.getValue().toString();
                String str7=num7.getValue().toString();
                String str8=num8.getValue().toString();
            	str=str1+" "+str2+" "+str3+" "+str4+" "+str5+" "+str6+" "+str7+" "+str8;
            	System.out.println(str);
            	
            	double sum=Double.parseDouble(str8);
            	
            	user4.setNum(str);
            	UserDao userDao = DaoFactory.getUserDaoInstance();
            	
            
            	User u=new User();
            	try {
					u=userDao.QuerryByUsername(user4.getUsername());
				} catch (Exception e2) {
					// TODO Auto-generated catch block
					e2.printStackTrace();
				}
            	
            	
            	double money=u.getMoney()-7*sum*2;
            	user4.setMoney(money);
            	
            	
            	
            	
            	
            	
            	try {
					userDao.Update(user4);
				} catch (Exception e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
            	
            	JOptionPane.showMessageDialog(null, "购买成功！");
            }
        });

        bt2.setText("返回");
        bt2.addActionListener(new ActionListener() {
        	@Override
            public void actionPerformed(ActionEvent e) {
            	setVisible(false);
            }
        });
        bt3.setText("随机选号");
        bt3.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
            	 ArrayList<Integer> list = new ArrayList<Integer>();
                 Random random = new Random();
                 int i = 0, num = 0;
                 while (i < 7) {
                         num = random.nextInt(35) + 1;
                         if (!list.contains(num)) {
                                 list.add(num);
                         } 
                         else 
                         {
                        	 continue;
                        }
                         
                         i++;
                 }
        
                 num1.setValue(list.remove(0));
                 num2.setValue(list.remove(0));
                 num3.setValue(list.remove(0));
                 num4.setValue(list.remove(0));
                 num5.setValue(list.remove(0));
                 num6.setValue(list.remove(0));
                 num7.setValue(list.remove(0));
                 
                 
            }
        
            
        
			
        });

        
       
        GroupLayout layout = new GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(30, 30, 30)
                                .addComponent(label1))
                            .addGroup(layout.createSequentialGroup()
                                .addContainerGap()
                                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.CENTER)
                                    .addComponent(num1, GroupLayout.PREFERRED_SIZE, 48, GroupLayout.PREFERRED_SIZE)
                                    .addComponent(label2)
                                    .addComponent(num5, GroupLayout.PREFERRED_SIZE, 48, GroupLayout.PREFERRED_SIZE)
                                    .addComponent(label6))
                                .addGap(25, 25, 25)
                                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.CENTER)
                                    .addComponent(label7)
                                    .addComponent(num6, GroupLayout.PREFERRED_SIZE, 48, GroupLayout.PREFERRED_SIZE)
                                    .addComponent(label3)
                                    .addComponent(num2,GroupLayout.PREFERRED_SIZE, 48, GroupLayout.PREFERRED_SIZE))
                                .addGap(25, 25, 25)
                                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.CENTER)
                                    .addComponent(label8)
                                    .addComponent(num7,GroupLayout.PREFERRED_SIZE, 48, GroupLayout.PREFERRED_SIZE)
                                    .addComponent(label4)
                                    .addComponent(num3, GroupLayout.PREFERRED_SIZE, 48, GroupLayout.PREFERRED_SIZE))
                                .addGap(25, 25, 25)
                                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.CENTER)
                                    .addComponent(label5)
                                    .addComponent(num4, GroupLayout.PREFERRED_SIZE, 48, GroupLayout.PREFERRED_SIZE)
                                    .addComponent(label9)
                                    .addComponent(num8, GroupLayout.PREFERRED_SIZE, 48, GroupLayout.PREFERRED_SIZE))))
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addGroup(GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addContainerGap()
                        .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(bt3)
                        .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(bt1)
                        .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(bt2)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(label1)
                .addGap(25, 25, 25)
                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                    .addComponent(num1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                    .addComponent(num2, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                    .addComponent(num3, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                    .addComponent(num4, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                    .addComponent(label2)
                    .addComponent(label3)
                    .addComponent(label4)
                    .addComponent(label5))
                .addGap(20, 20, 20)
                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                    .addComponent(num5, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                    .addComponent(num6, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                    .addComponent(num7, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                    .addComponent(num8, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                    .addComponent(label6)
                    .addComponent(label7)
                    .addComponent(label8)
                    .addComponent(label9))
                .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, 15, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                    .addComponent(bt1)
                    .addComponent(bt3)
                   
                    .addComponent(bt2))
                .addContainerGap())
        );

        pack();
       
    }
}
